﻿using System;
using System.Text;
using System.Collections;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Element;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler
{
  /// <summary>
  /// 简单文本的Handler
  /// </summary>
  public class SimpleTextHandler : HandlerBase
  {
    /// <summary>
    /// 编译后的文档模板内容生成函数
    /// </summary>
    protected Func<object, object, ITemplateRenderContext, string> _funContentRender = null;

    public override void DoRender(StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context)
    {
      if (_funContentRender == null)
      {
        lock (this)
        {
          if (_funContentRender == null)
          {
            var render = this.Element.ContentRender;
            if (render.IsNotNullOrEmpty())
            {
              var content = (this.Element as SimpleText).Content;
              _funContentRender = render.CompileRender(content);
            }
          }
        }

      }
      if (_funContentRender != null)
      {
        writer.Append(_funContentRender(rootParamObject, parentParamObject, context));
      }
    }
  }
}
